#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int show_help() {
	printf("\n");
	printf("Usage: cparams [options [parameters]]\n");
	printf("\n");
	printf("Mandatory options:\n");
	printf(" -p|--param1                [value1 | value2] (Mandatory)\n");
	printf("\n");
	printf("Options:\n");
	printf(" --param2                   [value1 | value2]\n");
	printf(" --param3                   (value)\n");
	printf("\n");
	printf(" -l|--list, list files in directory\n");
	printf(" -h|--help, Print help\n");

	exit(0);
}

int in(char **arr, int len, char *target) {
	int i;
	for(i = 0; i < len; i++) {
		if(strncmp(arr[i], target, strlen(target)) == 0) {
		  return 1;
		}
	}
	return 0;
}

//int main(int argc, const char * argv[]) {
int main(int argc, char **argv) {
	
	char param_1[10] = { '\0' };
	char param_2[10] = { '\0' };
	char param_3[10] = { '\0' };
	
	/* Show help if no parameter is use */
	if(argv[1] == NULL || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
		show_help();
	}
	if(strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "--list") == 0) {
		show_help();
	}
	
	/* Parse parameters */
	int i = 1; // start at 1, to start from first parameter used
	int remaining = argc - 1; // argc starts from 0, as 0 is the command
	printf("INFO: Parameters entered: %i\n", argc - 1);
	while (i < argc) {
		remaining--; // remove 1 parameter for parameter name
		if (strcmp(argv[i],"-p") == 0 || strcmp(argv[i],"--param1") == 0) {
			i++; // add 1 to i, to get next parameter as value 
			strcpy(param_1, argv[i]);
			printf("INFO: param1 value: %s\n", param_1);
		} else if (strcmp(argv[i],"--param2") == 0) {
			i++; // add 1 to i, to get next parameter as value 
			strcpy(param_2, argv[i]);
			printf("INFO: param2 value: %s\n", param_2);
		} else if (strcmp(argv[i],"--param3") == 0) {
			i++; // add 1 to i, to get next parameter as value 
			strcpy(param_3, argv[i]);
			printf("INFO: param2 value: %s\n", param_3);
		} else {
			printf("ERROR: Too many arguments. Exiting");
			show_help();
		}
        // Statement
        remaining--; // remove 1 parameter for parameter value
        printf("INFO: Parameters remaining: %i\n", remaining);
        printf("INFO: Parameters checked: %i\n", i);
        i++; // add 1 to i, to get next parameter name
    }
    
	// set default values
	if(strlen(param_1) == 0) {
		printf("ERROR: parameter is mandatory. Exiting\n");
		show_help();
	}
	if(strlen(param_2) == 0) {
		strcpy(param_2, "value1");
		printf("INFO: Setting default parameter: %s\n", param_2);
	}
	if(strlen(param_3) == 0) {
		strcpy(param_3, "value1");
		printf("INFO: Setting default parameter: %s\n", param_3);
	}
	
	// parameter validation
	char *param_1_values[3] = { "value1", "value2", "value3" };
	if(!in(param_1_values, 3, param_1)) {
		printf("ERROR: value %s is not a valid option, valid options are: ", param_1);
		int values_amount = (int)( sizeof(param_1_values) / sizeof(param_1_values[0]) );
		for(i=0; i<values_amount; i++) {
			printf("%s ", param_1_values[i]);
		}
		printf("\n");
		exit(1);
	}
	
	char *param_2_values[2] = { "value1", "value2" };
	if(!in(param_2_values, 2, param_2)) {
		printf("ERROR: value %s is not a valid option, valid options are: ", param_2);
		int values_amount = (int)( sizeof(param_2_values) / sizeof(param_2_values[0]) );
		for(i=0; i<values_amount; i++) {
			printf("%s ", param_2_values[i]);
		}
		printf("\n");
		exit(1);
	}
	
	printf("param1 %s\n", param_1);
	printf("param2 %s\n", param_2);
	printf("param3 %s\n", param_3);
	
	return 0;
}
